package com.taotao.druid.visitor;

import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
import com.taotao.ds.at.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TableNameVisitor extends MySqlASTVisitorAdapter {


    private String suffix;

    public TableNameVisitor(String suffix) {
        this.suffix = suffix;
    }

    @Override
    public boolean visit(SQLExprTableSource x) {
        String tableName = x.getExpr().toString();
        tableName = tableName.replaceAll("`", "");

//        log.info("-------------tableName:{},suffix={}", tableName, suffix);
        if (StringUtils.isBlank(suffix)) {
            return true;
        }
        x.setExpr("`" + tableName + "_" + suffix + "`");
        return true;
    }
}
